package com.hzcx.app.simplechat.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.publicui.PublicRoundDialog;
import com.hzcx.app.simplechat.ui.publicui.interfaces.OnPublicMenuOnClickListener;
import com.hzcx.app.simplechat.ui.setting.contract.SettingPrivateContract;
import com.hzcx.app.simplechat.ui.setting.presenter.SettingPrivatePresenter;
import com.hzcx.app.simplechat.ui.user.bean.UserInfoBean;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 隐私设置
 */
public class SettingPrivateActivity extends BaseActivity<SettingPrivatePresenter> implements SettingPrivateContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_addfriend_verification)
    TextView tvAddfriendVerification;
    @BindView(R.id.tv_add_type)
    TextView tvAddType;
    @BindView(R.id.tv_black)
    TextView tvBlack;
    @BindView(R.id.tv_friend_stranger)
    TextView tvFriendStranger;
    @BindView(R.id.tv_commont)
    TextView tvCommont;
    @BindView(R.id.tv_commont_value)
    TextView tvCommontValue;
    @BindView(R.id.switch_verification)
    Switch switchVerification;
    @BindView(R.id.switch_allow)
    Switch switchAllow;

    private UserInfoBean userInfoBean;

    private String commentState;

    private boolean isVerificationReady = true;
    private boolean verificationState;

    private boolean isAllowReady = true;
    private boolean allowState;

    @Override
    public int getLayout() {
        return R.layout.activity_setting_private;
    }

    @Override
    public void initPresenter() {
        mPresenter = new SettingPrivatePresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("隐私设置");
    }

    @Override
    public void initData() {
        userInfoBean = UserInfoUtil.getUserInfo();

        if (userInfoBean != null && EmptyUtils.isNotEmpty(userInfoBean.getIs_verification()) && "1".equals(userInfoBean.getIs_verification())) {
            switchVerification.setChecked(true);
        }

        if (userInfoBean != null && EmptyUtils.isNotEmpty(userInfoBean.getIs_allow()) && "1".equals(userInfoBean.getIs_allow())) {
            switchAllow.setChecked(true);
        }

        if (userInfoBean != null && EmptyUtils.isNotEmpty(userInfoBean.getDy_auth()) && "1".equals(userInfoBean.getDy_auth())) {
            tvCommontValue.setText("仅自己");
        } else if (userInfoBean != null && EmptyUtils.isNotEmpty(userInfoBean.getDy_auth()) && "2".equals(userInfoBean.getDy_auth())) {
            tvCommontValue.setText("仅好友");
        } else {
            tvCommontValue.setText("所有人");
        }

        switchVerification.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!isVerificationReady) {
                return;
            }
            if (userInfoBean == null) {
                userInfoBean = UserInfoUtil.getUserInfo();
            }
            if (userInfoBean == null) {
                return;
            }
            if (b) {
                mPresenter.setPrivateConfig(SettingPrivateActivity.this, "1", userInfoBean.getIs_allow(), userInfoBean.getDy_auth(),
                        1);
            } else {
                mPresenter.setPrivateConfig(SettingPrivateActivity.this, "0", userInfoBean.getIs_allow(), userInfoBean.getDy_auth(),
                        1);
            }
            verificationState = b;
            isVerificationReady = false;
        });

        switchAllow.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!isAllowReady) {
                return;
            }
            if (userInfoBean == null) {
                userInfoBean = UserInfoUtil.getUserInfo();
            }
            if (userInfoBean == null) {
                return;
            }
            if (b) {
                mPresenter.setPrivateConfig(SettingPrivateActivity.this, userInfoBean.getIs_verification(), "1", userInfoBean.getDy_auth(),
                        2);
            } else {
                mPresenter.setPrivateConfig(SettingPrivateActivity.this, userInfoBean.getIs_verification(), "0", userInfoBean.getDy_auth(),
                        2);
            }
            allowState = b;
            isAllowReady = false;
        });
    }

    @OnClick({R.id.tv_commont, R.id.tv_black, R.id.tv_add_type})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_commont: //点赞评论权限
                showCommontDialog();
                break;

            case R.id.tv_black: // 黑名单
                startActivity(new Intent(this, BlackListActivity.class));
                break;

            case R.id.tv_add_type: // 查找我的方式
                startActivity(new Intent(this, SettingFindMyWayActivity.class));
                break;
        }
    }

    /**
     * 点赞评论权限弹窗
     */
    private void showCommontDialog() {
        List<String> list = new ArrayList<>();
        list.add("所有人");
        list.add("仅好友");
        list.add("仅自己");
        PublicRoundDialog roundDialog = new PublicRoundDialog(this, list, new OnPublicMenuOnClickListener() {
            @Override
            public void menuOnClick(int pos, String menu) {
                commentState = menu;
                switch (menu) {
                    case "所有人":
                        mPresenter.setPrivateConfig(SettingPrivateActivity.this, userInfoBean.getIs_verification(), userInfoBean.getIs_allow(),
                                "0", 3);
                        break;
                    case "仅自己":
                        mPresenter.setPrivateConfig(SettingPrivateActivity.this, userInfoBean.getIs_verification(), userInfoBean.getIs_allow(),
                                "1", 3);
                        break;
                    case "仅好友":
                        mPresenter.setPrivateConfig(SettingPrivateActivity.this, userInfoBean.getIs_verification(), userInfoBean.getIs_allow(),
                                "2", 3);
                        break;
                }
            }
        });
        roundDialog.show();
    }

    /**
     * 设置成功
     */
    @Override
    public void setSuccess(int tag) {
        showError("设置成功");
        if (tag == 3) {
            tvCommontValue.setText(commentState);
            switch (commentState) {
                case "所有人":
                    userInfoBean.setDy_auth("0");
                    break;
                case "仅自己":
                    userInfoBean.setDy_auth("1");
                    break;
                case "仅好友":
                    userInfoBean.setDy_auth("2");
                    break;
            }
        } else if (tag == 1) {
            isVerificationReady = true;
            if (verificationState) {
                userInfoBean.setIs_verification("1");
            } else {
                userInfoBean.setIs_verification("0");
            }
        } else if (tag == 2) {
            isAllowReady = true;
            if (allowState) {
                userInfoBean.setIs_allow("1");
            } else {
                userInfoBean.setIs_allow("0");
            }
        }
        UserInfoUtil.saveUser(userInfoBean);
    }

    /**
     * 设置失败
     */
    @Override
    public void setFail(int tag) {
        if (tag == 1) {
            switchVerification.setChecked(!switchVerification.isChecked());
            isVerificationReady = true;
        } else if (tag == 2) {
            switchAllow.setChecked(!switchAllow.isChecked());
            isAllowReady = true;
        }
    }
}
